<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace AdminModule;

use OverFish, Nette, AdminModule\Forms\MenuItemForm;

class MenuItemPresenter extends BaseAdminPresenter {
    
    public $title = "Obsah";
    private $id = False;
    protected $menuItem = "menu";
    private $menuId;
    
    public function renderDefault($menu_id) {
        $menu = $this->repositories->menu->find($menu_id);
        $this->heading = "Položky menu";
        $this->menuId = $menu_id;
        $this->template->menu = $menu;
        $this->template->items = $this->repositories->menuItem->getTreeForGrid($menu_id);
        $this->template->editHref = $this->link(":Admin:MenuItem:edit", array("menu_id" => $menu->getId()));
        //\Nette\Diagnostics\Debugger::dump($this->template->items);
    }
    
    public function renderNew($menu_id) {
        $menu = $this->repositories->menu->find($menu_id);
        $this->heading = "Nová položka menu"; 
        $this->menuId = $menu_id;
        $this->template->menu = $menu;
    }
    
    public function renderEdit($menu_id, $id) {
        $menu = $this->repositories->menu->find($menu_id);
        $this->heading = "Upravit položku menu"; 
        $this->menuId = $menu_id;
        $this->template->menu = $menu;
        $this->id = $id;
    }
    
    protected function createComponentMenuItemForm($name)
    {
        $form = new MenuItemForm($this, $name);
        
        if ($this->id) {
            $item = $this->repositories->menuItem->find($this->id);
            
            $form->setDefaults(array(
                "id" => $item->getId(),
                "id_menu" => $this->menuId,
                "id_menu_item" => $item->getId_menu_item(),
                "id_page" => $item->getId_page(),
                "priority" => $item->getPriority(),
                "title" => $item->getTitle(),
            ));
        } else {
            $form->setDefaults(array(
                "id_menu" => $this->menuId,
            ));
        }
    }
}
